package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDictData;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysThesauriWordCategory;
import com.ruoyi.system.service.ISysThesauriWordCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 叙词分类Controller
 * 
 * @author ruoyi
 * @date 2022-08-23
 */
@RestController
@RequestMapping("/system/thesauriWordCategory")
public class SysThesauriWordCategoryController extends BaseController
{
    @Autowired
    private ISysThesauriWordCategoryService sysThesauriWordCategoryService;

    /**
     * 查询叙词分类列表
     */
    @PreAuthorize("@ss.hasAnyPermi('system:thesauriWordCategory:list,system:thesauriWord:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysThesauriWordCategory sysThesauriWordCategory)
    {
        startPage();
        List<SysThesauriWordCategory> list = sysThesauriWordCategoryService.selectSysThesauriWordCategoryList(sysThesauriWordCategory);
        getChildData(list);
        return getDataTable(list);
    }
    private void getChildData(List<SysThesauriWordCategory> list){
        SysThesauriWordCategory childData = null;
        for (SysThesauriWordCategory data : list) {
            Long pid = data.getId();
            childData = new SysThesauriWordCategory();
            childData.setParentId(pid+"");
            List<SysThesauriWordCategory> childDataList = sysThesauriWordCategoryService.selectSysThesauriWordCategoryList(childData);
            childDataList = childDataList.stream().filter(x->!x.getParentId().equals(pid+"")).collect(Collectors.toList());
            getChildData(childDataList);
            if(childDataList.size() > 0){
                data.setChildren(childDataList);
            }
        }
    }


    /**
     * 导出叙词分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordCategory:export')")
    @Log(title = "叙词分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysThesauriWordCategory sysThesauriWordCategory)
    {
        List<SysThesauriWordCategory> list = sysThesauriWordCategoryService.selectSysThesauriWordCategoryList(sysThesauriWordCategory);
        ExcelUtil<SysThesauriWordCategory> util = new ExcelUtil<SysThesauriWordCategory>(SysThesauriWordCategory.class);
        return util.exportExcel(list, "叙词分类数据");
    }

    /**
     * 获取叙词分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysThesauriWordCategoryService.selectSysThesauriWordCategoryById(id));
    }

    /**
     * 新增叙词分类
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordCategory:add')")
    @Log(title = "叙词分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysThesauriWordCategory sysThesauriWordCategory)
    {
        return toAjax(sysThesauriWordCategoryService.insertSysThesauriWordCategory(sysThesauriWordCategory));
    }

    /**
     * 修改叙词分类
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordCategory:edit')")
    @Log(title = "叙词分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysThesauriWordCategory sysThesauriWordCategory)
    {
        return toAjax(sysThesauriWordCategoryService.updateSysThesauriWordCategory(sysThesauriWordCategory));
    }

    /**
     * 删除叙词分类
     */
    @PreAuthorize("@ss.hasPermi('system:thesauriWordCategory:remove')")
    @Log(title = "叙词分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> removeIds = new ArrayList<>();
        for (Long id : ids) {
            removeIds.add(id);
            getRemoveIds(id, removeIds);
        }
        removeIds = removeIds.stream().distinct().collect(Collectors.toList());
        Long[] removeIdsArr = new Long[removeIds.size()];
        for (int i = 0; i < removeIds.size(); i++) {
            removeIdsArr[i] = removeIds.get(i);
        }
        sysThesauriWordCategoryService.deleteSysThesauriWordCategoryByIds(removeIdsArr);
        return success();
    }

    //删除时获取子节点
    public void getRemoveIds(Long pid, List<Long> removeIds){
        SysThesauriWordCategory sysThesauriWordCategory = null;
        List<SysThesauriWordCategory> dataList = null;
        sysThesauriWordCategory = new SysThesauriWordCategory();
        sysThesauriWordCategory.setParentId(pid+"");
        dataList = sysThesauriWordCategoryService.selectSysThesauriWordCategoryList(sysThesauriWordCategory);
        dataList = dataList.stream().filter(x->!x.getId().equals(pid+"")).collect(Collectors.toList());
        if(dataList.size() > 0){
            List<Long> childIds = dataList.stream().map(x -> x.getId()).collect(Collectors.toList());
            for (Long childId : childIds) {
                removeIds.add(childId);
                getRemoveIds(childId, removeIds);
            }
        }
    }
}
